﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Attribute
{
    using System;
    class Program
    {
        static void Main(string[] args)
        {
            MyClass.Message("In Main function");
            Function1();
            Console.ReadLine();

        }

        static void Function1()
        {
            MyClass.Message("In Function1");
            Function2();
        }

        //Obsolete Attribute
        [Obsolete("Don't use old method！",false)]
        static void Function2()
        {
            MyClass.Message("In Function2");
        }
       
    }
    //Conditional Attribute
    public class MyClass
    {
        [Conditional("DEBUG")]
        public static void Message(string msg)
        {
            //Console.WriteLine(msg);
            HelpAttribute helpAttribute;

            foreach(var attr in typeof(AnyClass).GetCustomAttributes(true))
            {
                helpAttribute = attr as HelpAttribute;
                if(null != helpAttribute)
                {
                    Console.WriteLine("AnyClass Description: {0}", helpAttribute.Description);
                }
            }
        }
    }

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
    public class HelpAttribute : Attribute
    {
        protected string description;

        public HelpAttribute(string description_in)
        {
            this.description = description_in;
        }

        public string Description
        {
            get
            {
                return this.description;
            }
        }

        public string Name
        {
            get;
            set;
        }

    }

    //Attribute的参数类型是有限制的，只能是 value type, System.Type, object, enum 
    [Help("This is a do-nothing class", Name = "dunso")] 
    public class AnyClass
    {

    }
}
